import React from 'react';
import { View } from 'react-native';
import * as WeChat from "react-native-wechat"
import { Lt, Color, Label, Button, Nav, Jpp, Dialog, Navigator, Jx, cSys } from "./_utils";
import { Net, NET_RI } from "./_net"

// 微信支付
export class PageVipPay extends React.Component{
    constructor(props){
        super(props); 
        this.onPay = this.onPay.bind(this);
        cSys.checkWechatInstalled();
    }
    static navigationOptions = {
        ...Nav.options,
        headerTitle:"成为运营商",
    }
    onPay(){
        if(!cSys.isWechatInstalled(true)){
            return;
        }
        Net.request("wx/wechatOrderPay", null, {NET_RI}).then(r=>{
            if(r.data){
                WeChat.pay({
                    partnerId: r.data.partnerid,  // 商家向财付通申请的商家id
                    prepayId: r.data.prepayid,   // 预支付订单
                    nonceStr: r.data.noncestr,   // 随机串，防重发
                    timeStamp: r.data.timestamp,  // 时间戳，防重发
                    package: r.data.packageValue,    // 商家根据财付通文档填写的数据和签名
                    sign: r.data.sign,        // 商家根据微信开放平台文档对数据做的签名
                }).then((success)=>{
                    Dialog.msg1("支付完成, 等待审核");
                    Navigator.pop();
                    const { onPop } = this.props.navigation.state.params;
                    onPop && onPop();
                }).catch((error)=>{
                    // 取消支付也来到这
                    Dialog.toast("支付失效");
                    console.warn(Jx.stringify(error));
                })
            }
        });
    }
    render(){
        return <View style = {{flex:1, backgroundColor:Color.back}}>
            <Nav.StatusBar/>
            <View style = {{paddingVertical:30, backgroundColor:"white", alignItems:"center"}}>
                <Label style={{fontSize:27,color:Color.theme}}>¥ 2188</Label>
                <Label style={{color:Color.c6}}>永久成为{Jpp.gameName}运营商</Label>
            </View>
            <Button.FlexPure title={"微信支付"} onPress={this.onPay}/>
        </View>;
    }
}